package com.ac.cv.server.service;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.ac.cv.client.service.MailService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class MailServiceImpl extends RemoteServiceServlet implements MailService{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5179836168754658647L;

	@Override
	public String sendMail(String address, String subject, String content) {
		try{
		Properties props = new Properties();
			Session session = Session.getDefaultInstance(props);
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("anthony.chatellier@gmail.com", address));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("anthony.chatellier@gmail.com", "Anthony Chatellier"));
            msg.setSubject(subject);
            msg.setText(content.toString());
            Transport.send(msg);
    
        } catch (AddressException e) {
        	return e.getMessage();
        } catch (MessagingException e) {
        	return e.getMessage();
            
        } catch (UnsupportedEncodingException e) {
        	return e.getMessage();
            
		}
        return "0";
	}

}
